import werkzeug
from flask import render_template, request, jsonify
from werkzeug.exceptions import HTTPException

from . import sys



#
# @sys.app_errorhandler(400)
# def page_not_found(e):
#     #if request.accept_mimetypes.accept_json and \
#      #       not request.accept_mimetypes.accept_html:
#
#         response = jsonify({'error': 'not found'})
#         response.status_code = 400
#         return response
#     #return render_template('404.html'), 404
def errorHandlr():
    pass
#sys.register_error_handler(HTTPException,errorHandlr)



@sys.errorhandler(werkzeug.exceptions.HTTPException)
def handle_bad_request(e):

    if  hasattr(e,'data') and e.data and  hasattr(e.data,'message') and e.data['message']:
        msg = str(e.data['message'])
    else:
        msg = e.name or 'Bad Request'

    return jsonify({'message':msg,'code':e.code}),400
